package com.lavida.project.system.emp.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lavida.common.utils.poi.ExcelUtil;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.domain.AjaxResult;
import com.lavida.project.common.DownLoadUtils;
import com.lavida.project.common.FileUpload;
import com.lavida.project.system.emp.domain.Emp;
import com.lavida.project.system.emp.domain.Empleave;
import com.lavida.project.system.emp.domain.Roster;
import com.lavida.project.system.emp.service.EmpService;
import com.lavida.project.system.emp.service.EmpleaveService;
import com.lavida.project.system.emp.service.RosterService;
import com.lavida.project.system.review.domain.Review;
import com.lavida.project.system.review.service.ReviewService;
import com.lavida.project.wage.domain.NewWageAccount;
/**
 * 	离职交接表
 * @author abh67
 *
 */
@Controller
@RequestMapping("system/empleave")
public class EmpleaveController {
	private String prefix = "system/emp";
	@Autowired
	private EmpService empService;
	
	@Autowired
	private ReviewService reviewService;
	
	@Autowired
	private RosterService rosterService;
	
	@Autowired
	private EmpleaveService empleaveService;
	
	@RequestMapping("/leave")
	public String empleave(HttpServletRequest request,Integer job_id) {
		Emp emp = empService.selectByJob_id(job_id);
		Roster roster = rosterService.getRoster(job_id);
		Review review = reviewService.selectReviewById(job_id);
		Empleave empleave = empleaveService.selectEmpleaveById(job_id);
		if(emp != null) {
			request.setAttribute("review", review);
			request.setAttribute("emp", emp);
			request.setAttribute("Roster", roster);
		}
		request.setAttribute("empleave",empleave);
		return prefix+"/empleave";
	}
	@Log(title = "离职单", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Integer job_id) {
		List<Empleave> empleave2  = empleaveService.selectEmpleaveId(job_id);
		ExcelUtil<Empleave> util = new ExcelUtil<Empleave>(Empleave.class);
		return util.exportExcel(empleave2, "离职单");
	}
	//文件上传
	@RequestMapping("/update")
	public String update(int job_id,HttpServletRequest request,Empleave empleave) {
		Emp emp1= empService.selectEmpByempno(job_id);
		emp1.setStatus("离职");
		empService.updateEmp(emp1);
		return "redirect:leave?job_id="+job_id;
	}
	//文件下载
	@RequestMapping("/download")
	public void download(String filename,HttpServletRequest request,HttpServletResponse response) {
		String string = request.getHeader("User-Agent");
		String Uploadfilename = null;
		//System.out.println("filename>>>>>>>>>>>>"+filename);
		try {
			
			Uploadfilename = DownLoadUtils.getFileName(string,String.format(filename));
			response.setHeader("Content-disposition", "attachment;filename=" + Uploadfilename);
			String fileVmPath = FileUpload.filePath(request);

			ServletOutputStream s = response.getOutputStream();
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					new FileInputStream(String.format("%s/%s", fileVmPath, filename)));
			int a;
			byte[] bytes = new byte[1024];
			while ((a = bufferedInputStream.read(bytes)) != -1) {
				s.write(bytes, 0, a);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private String getFilenameUUID(MultipartFile file,HttpServletRequest request) {	
		
		 String path=FileUpload.filePath(request);
	     String originalFilename=file.getOriginalFilename(); 
	     
	     String uuid=UUID.randomUUID().toString().replace("-","");
	     String filename=uuid+"_"+originalFilename;
	     try {
	    	 file.transferTo(new File(path,filename));
	     }catch (Exception e) {
			// TODO: handle exception
		}
	     return filename;
	}

}
